/*------------------------------------------------------------------------------*\
**
**  adventure_treasure_map.cpp
**
**			Heroes IV
**	  Copyright 2001, The 3DO Company
**
\*------------------------------------------------------------------------------*/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "adventure_treasure_map.h"

#include "adventure_frame.h"
#include "adventure_object.h"
#include "convert_16_bit.h"
#include "direction.h"
#include "path_arrow.h"

/*
** Constructor for treasure map (map that opens when player
** gets all obelisks/oracles) - shows where artifact is.
*/
t_adventure_treasure_map::t_adventure_treasure_map( t_screen_rect const& rect, t_adventure_frame* frame_ptr, t_window* parent_ptr,
                t_level_map_point_2d point )
                      : t_adventure_map_window_base ( rect, frame_ptr, parent_ptr )
{
	t_window::init( rect );
	frame_ptr->set_view_level( point.level );
	t_map_renderer::set_view_level( point.level );
	t_map_renderer::move_view( t_screen_point(0,0), t_screen_point(get_width(), get_height()) );

    /*
    ** X marks the spot - put an X where the artifact is.
    ** Right now, an object is added to the map...maybe draw directly
    ** to the buffer?
    */
	t_adventure_map*        map_ptr = frame_ptr->get_map();
    t_adv_map_point         x_point = t_adv_map_point ( point, true );

	m_x_object_ptr = new t_path_arrow( k_direction_count, k_direction_count, k_path_arrow_green );
	m_x_object_ptr->place( *map_ptr, x_point );

    // Init image, just in case...
    draw_grayscale();
}

/*
** Convert current image to grayscale, save in buffer.
*/
void t_adventure_treasure_map::draw_grayscale()
{
    t_screen_rect rect = m_back_buffer->get_rect();

    /*
    ** Convert to grayscale.
    */
	t_abstract_bitmap16_ptr bitmap = m_back_buffer;
	t_abstract_bitmap16_ptr bitmap_save = m_back_buffer_save;

	int x;
	int y;
	int total;
	int       increment = bitmap->get_pitch() - rect.width() * 2;
	t_uint16* ptr = bitmap->get_data_ptr();
	t_uint16* ptr_save = bitmap_save->get_data_ptr();
    int red_val;
    int blue_val;
    int green_val;

	for (y = 0; y < rect.height(); y++)
	{
		for (x = 0; x < rect.width(); x++)
		{
            // Convert to grayscale(1/3 the sum of r, g, b)...
	        red_val = red_channel ( *ptr );
        	green_val = green_channel ( *ptr );
        	blue_val = blue_channel ( *ptr );
            
            total = ((red_val + green_val + blue_val)/3);
            *ptr_save++ = convert_to_16_bit (total, total, total);
            ptr++;
		}
		ptr = bitmap->byte_increment( ptr, increment );
		ptr_save = bitmap->byte_increment( ptr_save, increment );
	}
}

void t_adventure_treasure_map::paint( t_paint_surface& paint_surface )
{
    t_screen_rect rect = m_back_buffer->get_rect();

    /*
    ** Draw save image (grayscale).
    */
	paint_surface.draw( t_screen_point(0,0), *m_back_buffer_save, rect ); //m_back_buffer->get_rect() );

}

t_abstract_bitmap16_ptr t_adventure_treasure_map::create_back_buffer( t_screen_point const & view_size )
{
	m_back_buffer = new t_memory_bitmap16( view_size.x, view_size.y );
	m_back_buffer_save = new t_memory_bitmap16( view_size.x, view_size.y );
	return m_back_buffer;
}

void t_adventure_treasure_map::on_rects_dirtied( std::vector< t_screen_rect > const & dirtied_rects )
{
	int i;

	for (i = 0; i < dirtied_rects.size(); i++)
    {
	    update( dirtied_rects[i] );
		t_window::invalidate( dirtied_rects[i] );
    }
}

void t_adventure_treasure_map::on_view_moved( int new_view_level, t_screen_point const & new_view_pos,
		                                    t_screen_point const & new_view_size )
{
}

void t_adventure_treasure_map::on_close()
{
    // Remove X marker.
    m_x_object_ptr->destroy();

}

void t_adventure_treasure_map::left_button_up( t_mouse_event const& event )
{
}

